package util.pagingOpt.impl;

import dao.admins.impl.AnnouncementManImpl;
import pojo.Announcement;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AnnPagingImpl extends Paging {
    public AnnPagingImpl(int pageSize, String sqlCount, String sqlQuery) {
        super(pageSize, sqlCount, sqlQuery);
    }

    @Override
    public List<Announcement> getList() {
        return queryList();
    }

    private  List<Announcement> queryList(){
        List<Announcement> announcements = new ArrayList<>();
        Object[] params = {(pageEntity.getCurrentPage() - 1) * pageEntity.getPageSize(), pageEntity.getPageSize()};
        try(ResultSet rs = baseDao.executeSelect(sqlPaging, params)){
            while (rs.next()){
                Announcement announcement = new Announcement();
                AnnouncementManImpl.setAnn(announcement, rs.getLong("id"), rs.getString("aTitle"), rs.getString("aText"), rs.getTimestamp("aDate"));
                announcements.add(announcement);
            }
            baseDao.closeAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return announcements;
    }
}
